package com.summer.patterns;

public class Test {
	
	public static class Book{
		public boolean isNeedUnLock = false;
	}
	
	public static class User{
		
		public boolean isLackOfBalance(){
			return true;
		}
		
		public void paymentUnlock(){
			
		}
	}
	
	public static void main(String[] args) {
		User user = new User();
		Book book = new Book();
		TaskCenter bookRead = new TaskCenter();
		bookRead.addTask(new ObtainPreviewInfo(book))
		.addTask(new DetermineUnlock(book))
		.addTask(new BalanceCheck(user))
		.addTask(new CoinsUnlock(user,book))
		.addTask(new AdvertisingUnlock(book))
		.addTask(new ObtainDetailsInfo(book))
		.execute();
	}
	
	public static class ObtainPreviewInfo extends TaskBase{
		
		private final Book book;
		
		public ObtainPreviewInfo(Book book) {
			this.book = book;
		}
		
		@Override
		protected void onDoTask() {
			System.out.println("获取到书籍预览信息");
			complete();
		}
		
	}
	
	public static class DetermineUnlock extends TaskBase{

		private final Book book;
		
		public DetermineUnlock(Book book) {
			this.book = book;
		}
		
		@Override
		protected void onDoTask() {
			System.out.println("书籍需要解锁");
			book.isNeedUnLock = true;
			
			complete();
		}
		
	}
	
	public static class BalanceCheck extends TaskBase{
		
		private final User user;
		
		public BalanceCheck(User user) {
			this.user = user;
		}

		@Override
		protected void onDoTask() {
			System.out.println("检查用户余额");
			if(user.isLackOfBalance())
				System.out.println("用户余额不足");
			else
				System.out.println("用户余额足够");
			complete();
		}
		
	}
	
	public static class CoinsUnlock extends TaskBase{
		
		private final User user;
		private final Book book;
		
		public CoinsUnlock(User user,Book book) {
			this.user = user;
			this.book = book;
		}

		@Override
		public boolean onPrepare() {
			return book.isNeedUnLock && !user.isLackOfBalance();
		}
		
		@Override
		protected void onDoTask() {
			

			boolean unLock = true;
			if(unLock){
				System.out.println("使用金豆解锁书籍");
				user.paymentUnlock();
			}else{
				System.out.println("不使用金豆解锁书籍");
			}

			book.isNeedUnLock = !unLock;
			
			complete();
		}
		
	}
	
	public static class AdvertisingUnlock extends TaskBase{

		private final Book book;
		
		public AdvertisingUnlock(Book book) {
			this.book = book;
		}
		
		@Override
		public boolean prepare() {
			return book.isNeedUnLock;
		}
		
		@Override
		protected void onDoTask() {
			System.out.println("观看广告解锁书籍");
			book.isNeedUnLock = false;
			complete();
		}
		
	}
	
	public static class ObtainDetailsInfo extends TaskBase{

		private final Book book;
		
		public ObtainDetailsInfo(Book book) {
			this.book = book;
		}
		
		@Override
		public boolean prepare() {
			return !book.isNeedUnLock;
		}
		
		@Override
		protected void onDoTask() {
			System.out.println("获取到书籍详细信息");

			complete();
		}
		
	}
	
	
}
